var e = getApp();

Component({
    properties: {
        goods_plugin: {
            type: Object,
            value: {}
        },
        goodsInfo: {
            type: Object,
            value: {}
        }
    },
    data: {
        personnel: [],
        radio: 0,
        personnel_obj: {},
        dateArr: [],
        day_index: 0,
        time_index: null,
        day_obj: {},
        time_obj: {},
        service_text: "服务",
        reserve_obj_text: "预约人员"
    },
    lifetimes: {
        attached: function() {
            var e = this;
            if (wx.getStorage({
                key: "yz_basic_info",
                success: function(t) {
                    e.setData({
                        service_text: t.data.lang.reserve_simple.service || "服务",
                        reserve_obj_text: t.data.lang.reserve_simple.reserve_obj || "预约人员"
                    });
                }
            }), this.data.goods_plugin && this.data.goods_plugin.reserve_simple_goods && this.data.goods_plugin.reserve_simple_goods.objs) {
                var t = this.data.goods_plugin.reserve_simple_goods.objs, a = {
                    personnel: t
                };
                t.length > 0 && (a.radio = t[0].id, a.personnel_obj = t[0]), this.setData(a), t.length > 0 && this.getWorkTime(t[0].id);
            }
        }
    },
    methods: {
        getWeek: function(e) {
            var t = e.split("-"), a = new Date(t[0], parseInt(t[1] - 1), t[2]);
            return "周" + "日一二三四五六".charAt(a.getDay());
        },
        onChangeRadio: function(e) {
            this.setData({
                radio: e.detail
            });
            var t = this.data.personnel.find(function(t) {
                return t.id == e.detail;
            });
            this.setData({
                personnel_obj: t
            }), this.getWorkTime(e.detail);
        },
        setDayIndex: function(e) {
            var t = e.currentTarget.dataset, a = t.item, i = t.index;
            0 != a.status && this.setData({
                day_index: i,
                day_obj: a,
                time_index: null
            });
        },
        setTimeIndex: function(e) {
            var t = e.currentTarget.dataset, a = t.item, i = t.index;
            0 != a.status && this.setData({
                time_index: i,
                time_obj: a
            });
        },
        gotoOrder: function() {
            if (null == this.data.time_index) return e.tips("请选择预约时间");
            var t = this.data.personnel_obj, a = t.service_id, i = t.id, r = t.goods_option_id, s = t.name, o = {
                service_id: a,
                obj_id: i,
                begin_time: this.data.dateArr[this.data.day_index].work_date + " " + this.data.dateArr[this.data.day_index].periods[this.data.time_index].period_time_start_str,
                name: s
            };
            wx.navigateTo({
                url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=-2&total=1&optionsId=" + r + "&goodsId=" + this.data.goodsInfo.id + "&reserve_order=" + JSON.stringify(o)
            });
        },
        gotoUserDe: function(e) {
            var t = e.currentTarget.dataset.id;
            wx.navigateTo({
                url: "/packageF/timeAppointment/AppointmentUserDe/AppointmentUserDe?id=" + t
            });
        },
        getWorkTime: function(t) {
            var a = this, i = this.data.personnel.find(function(e) {
                return e.id == t;
            });
            if (console.log(i), i) {
                var r = e.getNetAddresss("plugin.reserve-simple.frontend.work-time.get-service-obj-periods");
                e._getNetWork({
                    url: r,
                    data: {
                        obj_id: i.id,
                        service_id: i.service_id
                    },
                    success: function(t) {
                        var i = t.data;
                        if (1 != i.result) return e.tips(i.msg);
                        var r = i.data;
                        r.forEach(function(e) {
                            e.week = a.getWeek(e.work_date), e.day = e.work_date.substr(-2, 2), e.month = e.work_date.substr(-5, 5);
                        }), console.log(r), a.setData({
                            dateArr: r,
                            day_obj: r[0]
                        });
                    }
                });
            }
        },
        GetDateStr: function(e) {
            for (var t = [], a = 0; a < e; a++) {
                var i = new Date();
                i.setDate(i.getDate() + a);
                var r = i.getFullYear(), s = i.getMonth() + 1 < 10 ? "0" + (i.getMonth() + 1) : i.getMonth() + 1, o = i.getDate() < 10 ? "0" + i.getDate() : i.getDate(), n = {
                    day: o,
                    week: this.getWeek(r + "-" + s + "-" + o)
                };
                t.push(n);
            }
            return t;
        }
    }
});